<?php
if(!defined('ACCESS')) {exit('Access denied.');}

class TouristOrder extends Base{
	// 表名
	private static $table_name = 'tourist_order';
	// 查询字段
	private static $columns = array('tr_id', 'tourist_id', 'tourist_name','tourist_type', 'tourist_addr', 'tourist_pay','self_drive', 'tr_create_time', 'tourist_start_time','tourist_end_time','is_pay');
	//状态定义
	const ACTIVE = 1;
	const DEACTIVE = 0;
	public static function getTableName(){
		return self::$table_name;
	}
	
	public static function getTouristOrderById($tr_id) {
		$db=self::__instance();
		$sql= "select *  from ".self::getTableName() ." where tr_id='$tr_id'";
		$list = $db->query($sql)->fetch();
		if ($list) {
			return $list;
		}
		return array ();
	}


	public static function getTouristOrders( $start ='' ,$page_size='' ) {
		$db=self::__instance();
		$limit ="";
		if($page_size){
			$limit =" limit $start,$page_size ";
		}
		$sql = "select *  from ".self::getTableName()."  order by tr_id asc $limit";
	
		$list=$db->query($sql)->fetchAll();
		if ($list) {
			return $list;
		}
		return array ();
	}
	
	public static function search($user_group ,$user_name, $start ='' ,$page_size='' ) {
		$db=self::__instance();
		$limit ="";
		$where = "";
		if($page_size){
			$limit =" limit $start,$page_size ";
		}
		if($user_group >0  && $user_name!=""){
			$where = " where u.user_group=$user_group and u.user_name like '%$user_name%'";
		}else{
			if($user_group>0){
				$where = " where u.user_group=$user_group ";
			}
			if($user_name!=""){
				$where = " where u.user_name like '%$user_name%' ";
			}
		}
		$sql = "select * ,coalesce(g.group_name,'已删除') from ".self::getTableName()." u left join ".UserGroup::getTableName()." g on u.user_group = g.group_id $where order by u.user_id desc $limit";
		
		$list=$db->query($sql)->fetchAll();
		if(!empty($list)){
			foreach($list as &$item){
				
				$item['login_time']=Common::getDateTime($item['login_time']);
			}
		}
		if ($list) {
			return $list;
		}
		return array ();
	}
		
	
	public static function updateTouristOrder($tr_id,$holder_data) {
		
		if (! $holder_data || ! is_array ( $holder_data )) {
			return false;
		}
		$db=self::__instance();
		$condition=array("tr_id"=>$tr_id);
		
		$id = $db->update ( self::getTableName(), $holder_data, $condition );
		return $id;
	}
	
	
	public static function addTouristOrder($stock_data) {
		if (! $stock_data || ! is_array ( $stock_data )) {
			return false;
		}
		$db=self::__instance();
		$id = $db->insert ( self::getTableName(), $stock_data );
		
		return $id;
	}
	
	public static function delTouristOrder($tr_id) {
		if (! $tr_id || ! is_numeric ( $tr_id )) {
			return false;
		}
		$db=self::__instance();
		$condition = array("tr_id"=>$tr_id);
		$result = $db->delete ( self::getTableName(), $condition );
		return $result;
	}
	
	
	
	public static function count($condition = '') {
		$db=self::__instance();
		$num = $db->count ( self::getTableName(), $condition );
		return $num;
	}
}